package xyz.cerberu.cet.global.configuration;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author β世界
 * Created on 18:20
 * @Description
 */
@Configuration
public class CacheManagerConfig {

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory){
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                // 设置默认的key存活时间为五分钟
                .entryTtl(Duration.ofSeconds(300))
                // 设置key的序列化
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                // 设置value的序列化
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getValuesJacksonSerializer()));

        // 自定义特定的存活时间
        Map<String,RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        // resource组的缓存存活时间为半个
        redisCacheConfigurationMap.put("resource",redisCacheConfiguration.entryTtl(Duration.ofSeconds(1800)));
        // anime组的缓存存活时间为30个小时
        redisCacheConfigurationMap.put("anime",redisCacheConfiguration.entryTtl(Duration.ofSeconds(3000)));

        return RedisCacheManager.builder(connectionFactory)
                // 默认配置（强烈建议配置上）
                .cacheDefaults(redisCacheConfiguration)
                // 不同cache的个性化配置
                .withInitialCacheConfigurations(redisCacheConfigurationMap)
                .build();
    }

    /**
     * @author β世界 on 8:38 2021/3/15
     * 创建序列化器
     * @return org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer<java.lang.Object>
     **/
    private Jackson2JsonRedisSerializer<Object> getValuesJacksonSerializer(){
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        return jackson2JsonRedisSerializer;
    }
}
